package com.shop.loan.service.impl;

import com.github.pagehelper.PageHelper;
import com.shop.loan.mapper.ClassifyByMybatisMapper;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.ClassifyByMybatis;
import com.shop.loan.model.vo.ClassifyVo;
import com.shop.loan.service.ClassifyService;
import com.shop.loan.model.Classify;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.config.QueryExpression;
import xyz.erupt.annotation.query.Condition;
import xyz.erupt.core.query.EruptQuery;
import xyz.erupt.core.service.EruptCoreService;
import xyz.erupt.core.service.IEruptDataService;
import xyz.erupt.core.view.Page;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Resource
    IEruptDataService eruptDataService;

    @Resource
    ClassifyByMybatisMapper classifyByMybatisMapper;

    @Override
    public ResResult list(ClassifyVo classifyVo) {
        PageHelper.startPage(1, 50);

        ClassifyByMybatis search = new ClassifyByMybatis();
        search.setTopClassifyId(classifyVo.getTopClassifyId());
        com.github.pagehelper.Page<ClassifyByMybatis> classifyByMybatis = classifyByMybatisMapper.selectByList(search);
        return new ResResult(classifyByMybatis, classifyByMybatis.getPages(), classifyByMybatis.getTotal());
    }


    @Override
    public List<Long> getClassifyDown(Long classifyId) {
        List<Long> idList = new ArrayList<>();

        List<Condition> conditionList = new ArrayList<>();
        conditionList.add(new Condition("top_classify_id", classifyId, QueryExpression.EQ));
        EruptQuery goodsQuery = EruptQuery.builder().conditions(conditionList).build();
        Page page = eruptDataService.queryList(EruptCoreService.getErupt(Classify.class.getSimpleName()),
                new Page(1, 30), goodsQuery);
        page.getList().forEach(map -> {
            idList.add(Long.valueOf(map.get("id").toString()));
        });
        idList.add(classifyId);
        return idList;
    }
}
